﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using System.Windows.Media;
using System.Windows.Media.Media3D;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;

using Bling.Core;
using Bling.WPF;
using Bling.WPF.Util;
using Bling.Slides;
using Bling.Util;
using Bling.Shaders;
using Bling.Graphics;

namespace Bling.Tutorial {
  public class Driver : SlideHolder {
    public Driver() {
      {
        var sliderA = new SliderBl();
        var sliderB = new SliderBl();
        sliderA.Value.TwoWay = sliderB.Value;
      }


      new Bling.Shaders.WPFTextureProvider();
      //this["Fan"] = () => new Fan();

      this["Title"] = () => new Title();
      this["Curves"] = () => new CurveTest();
      this["Constraints"] = () => new Constraints();
      this["Matrix"] = () => new MatrixTest();

      this["Sphere3D"] = () => new VirtualSphereTest();

      this["Shaders"] = () => new Shaders();
      this["Lighting"] = () => new LightingExample();
      this["Web Browser"] = () => new BrowserTest();
      this["MetaBall"] = () => new MetaBall();
      this["Transitions"] = () => new TransitionsTest();
      this["Ripple"] = () => new RipplesTest();
      this["Tweens"] = () => new Tweens();
      this["Pie"] = () => new PieTest();
      this["Menus"] = () => new Menus();
      this["Flip"] = () => new BookFlipDemo.FreeFlipCanvas();
      this["Tables"] = () => new Tables();
      this["Physics"] = () => new PhysicsTest();
      Loaded += (x, y) => {
        Init();
      };
    }
    [STAThread]
    public static void Main(string[] args) {
      var win = new Window() { Title = "Bling Tutorial", Content = new Driver() };
      new Application().Run(win);
      return;
    }
  }
  public class Title : Canvas, ICanStart {
    private readonly LabelBl title;
    public static readonly ImageBrushBl Autumn = new ImageBrush(new BitmapImage("Resources/Autumn.jpg".MakePackUri(typeof(RipplesTest)))); 
    /*
    public static readonly GetProperty<Title, double> AnimateProperty = "Animate".NewProperty<Title, double>();
    public DoubleBl Animate {
      get { return AnimateProperty[this]; }
      set { Animate.Bind = value; }
    }*/
    private readonly DoubleBl Animate;
    public Title() {
      CanvasBl MainCanvas = this.Bl();
      Animate = this.Property(new DoubleBl());
      title = new LabelBl(this) {
        Font = { Size = 120, Weight = FontWeights.SemiBold, },
        CenterPosition = this.Bl().CenterSize,
        Content = "Bling Tutorial".Bl(),
        //RenderTransform = { Rotate = 45.ToDegrees() * -1d + 90.ToDegrees() * Animate * Animate * Animate },
      };
      Tex2D noise = 0.5.Bl().Lerp(ShaderLib.Noise, Autumn);
      title.Effect.Custom = (input,uv) => {
        var uv0 = new PointBl((uv.X + 13d * Animate), (uv.Y + 17d * Animate)) % 1d;
        return new ColorBl(noise[uv0].ScRGB, input[uv].ScA);
      };

      return;
    }
    public void Start() {
      Animate.Animate().Forever().AutoReverse().To = 1;
    }
    public void Stop() {
      Animate.Stop(false);
    }
  }
}
